package com.sf.fy.uav.task.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sf.fy.uav.common.enums.TcOrderStatusEnum;
import com.sf.fy.uav.common.request.operate.ErrandOrderReq;
import com.sf.fy.uav.common.response.order.ErrandOrderStatusResp;
import com.sf.fy.uav.common.response.order.task.TaskStatusLogResponse;
import com.sf.fy.uav.common.utils.BeanUtil;
import com.sf.fy.uav.common.utils.ResultCode;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.entity.ErrandOrder;
import com.sf.fy.uav.order.service.IOrderInfoService;
import com.sf.fy.uav.rest.SysTcRest;
import com.sf.fy.uav.service.IErrandOrderService;
import com.sf.fy.uav.task.entity.TaskStatusLog;
import com.sf.fy.uav.task.mapper.TaskStatusLogMapper;
import com.sf.fy.uav.task.service.ITaskStatusLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 状态日志 服务实现类
 * </p>
 *
 * @author LTQ
 * @since 2023-04-18
 */
@RequiredArgsConstructor
@Service
public class TaskStatusLogServiceImpl extends ServiceImpl<TaskStatusLogMapper, TaskStatusLog> implements ITaskStatusLogService {

    @Autowired
    private TaskStatusLogMapper taskStatusLogMapper;

    private final IOrderInfoService iOrderInfoService;
    private final IErrandOrderService iErrandOrderService;
    private final SysTcRest sysTcRest;

    @Override
    public void addTaskStatusLog(TaskStatusLog taskStatusLog) {
        taskStatusLogMapper.insert(taskStatusLog);
    }

    @Override
    public List<TaskStatusLog> selectListByTaskId(String taskId) {
        QueryWrapper<TaskStatusLog> taskStatusLogQueryWrapper = new QueryWrapper<>();
        taskStatusLogQueryWrapper.eq("task_id", taskId);
        taskStatusLogQueryWrapper.orderByDesc("update_time");
        taskStatusLogQueryWrapper.orderByAsc("status");
        List<TaskStatusLog> taskStatusLogs = taskStatusLogMapper.selectList(taskStatusLogQueryWrapper);
        return taskStatusLogs;
    }

    @Override
    public Rs<ErrandOrderStatusResp> getStatusLogList(ErrandOrderReq errandOrderReq) {
        //判断订单号是否为空
        if (StrUtil.isBlank(errandOrderReq.getId())) {
            return Rs.success();
        }
        //查询订单信息
/*        OrderInfo orderInfo = iOrderInfoService.getOne(new LambdaQueryWrapper<OrderInfo>().eq(OrderInfo::getTcWaybillNo, errandOrderReq.getFyWayBillNo()));
        if (ObjectUtil.isNull(orderInfo)) {
            return Rs.success();
        }*/

        //查询同城跑腿订单信息
        ErrandOrder errandOrder = iErrandOrderService.getById(errandOrderReq.getId());
        if (ObjectUtil.isNull(errandOrder)) {
            return Rs.success();
        }
        //查询是否存在同城跑腿订单编号，否则判定为地勤揽件订单，无法查询同城订单状态流
        if (StrUtil.isBlank(errandOrder.getErrandOrderSn())) {
            return Rs.failed(ResultCode.FAIL, "地勤揽件订单无法查询订单状态记录");
        }

        //查询订单状态记录
        List<TaskStatusLogResponse> taskStatusLogs = sysTcRest.getListOrderFeed(errandOrder.getErrandOrderSn());
        if (CollUtil.isEmpty(taskStatusLogs)) {
            return Rs.success();
        }
        List<TaskStatusLogResponse> distributionOrderListRes = BeanUtil.copyProperties(taskStatusLogs, TaskStatusLogResponse.class);
        distributionOrderListRes.forEach(item -> {
            String name = TcOrderStatusEnum.getName(item.getStatus());
            item.setStatusName(name);
        });

        ErrandOrderStatusResp errandOrderStatusResp = ErrandOrderStatusResp.builder()
                .orderSn(errandOrder.getOrderSn())
                .errandOrderSn(errandOrder.getErrandOrderSn())
                .taskStatusLogRespList(distributionOrderListRes).build();

/*        //查询订单状态变更列表
        List<TaskStatusLog> taskStatusLogs = taskStatusLogMapper.selectList(new LambdaQueryWrapper<TaskStatusLog>().
                eq(TaskStatusLog::getTaskId, orderInfo.getId())
                .orderByAsc(TaskStatusLog::getCreateTime));
        List<TaskStatusLogResponse> distributionOrderListRes = BeanUtil.copyProperties(taskStatusLogs, TaskStatusLogResponse.class);
        distributionOrderListRes.forEach(item ->{
            String name = OrderStatusEnum.getName(item.getStatus());
            item.setStatusName(name);
        });
        ErrandOrderStatusResp errandOrderStatusResp = ErrandOrderStatusResp.builder()
                .orderSn(errandOrder.getOrderSn())
                .errandOrderSn(errandOrder.getErrandOrderSn())
                .taskStatusLogRespList(distributionOrderListRes).build();*/
        return Rs.success(errandOrderStatusResp);
    }

}
